﻿using System;

namespace CallGraphExtractor
{
    [Flags]
    public enum CallTypes
    {
        None = 0x0,
        /// <summary>
        /// Used when the destination address is fixed at the time the CIL is linked.
        /// </summary>
        Call = 0x1,
        /// <summary>
        /// Used when the destination address is calculated at run time.
        /// </summary>
        Calli = 0x2,
        /// <summary>
        /// Uses the class of an object, known only at run time, to determine the method to be called.
        /// </summary>
        Callvirt = 0x4,
        /// <summary>
        /// Konstruktor hívás.
        /// </summary>
        NewObj = 0x8,
        /// <summary>
        /// Push the value of field of object (or value type) obj, onto the stack.
        /// </summary>
        LdFld = 0x16,
        /// <summary>
        /// Create a new array with elements of type etype.
        /// </summary>
        NewArr = 0x32,
        //TODO: property-k, field-ek, események
    }

    public static class CallTypesExtension
    {
        public static CallTypes Parse(this CallTypes callTypes, int value)
        {
            return Parse(value);
        }

        public static CallTypes Parse(int value)
        {
#if MONO_CECIL
            value += 1;
#endif
            switch (value)
            {
                case 40:
                    return CallTypes.Call;
                case 41:
                    return CallTypes.Calli;
                case 111:
                    return CallTypes.Callvirt;
                case 115:
                    return CallTypes.NewObj;
                case 123:
                    return CallTypes.LdFld;
                case 141:
                    return CallTypes.NewArr;
                default:
                    return CallTypes.None;
            }
        }
    }
}
